package cn.iocoder.yudao.module.fm.service.subgradedefaultdata;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.fm.controller.admin.subgradedefaultdata.vo.*;
import cn.iocoder.yudao.module.fm.dal.dataobject.subgradedefaultdata.SubgradeDefaultDataDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fm.convert.subgradedefaultdata.SubgradeDefaultDataConvert;
import cn.iocoder.yudao.module.fm.dal.mysql.subgradedefaultdata.SubgradeDefaultDataMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.fm.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 路基 默认数据 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class SubgradeDefaultDataServiceImpl implements SubgradeDefaultDataService {

    @Resource
    private SubgradeDefaultDataMapper subgradeDefaultDataMapper;

    @Override
    public Long createSubgradeDefaultData(SubgradeDefaultDataCreateReqVO createReqVO) {
        // 插入
        SubgradeDefaultDataDO subgradeDefaultData = SubgradeDefaultDataConvert.INSTANCE.convert(createReqVO);
        subgradeDefaultDataMapper.insert(subgradeDefaultData);
        // 返回
        return subgradeDefaultData.getId();
    }

    @Override
    public void updateSubgradeDefaultData(SubgradeDefaultDataUpdateReqVO updateReqVO) {
        // 校验存在
        validateSubgradeDefaultDataExists(updateReqVO.getId());
        // 更新
        SubgradeDefaultDataDO updateObj = SubgradeDefaultDataConvert.INSTANCE.convert(updateReqVO);
        subgradeDefaultDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteSubgradeDefaultData(Long id) {
        // 校验存在
        validateSubgradeDefaultDataExists(id);
        // 删除
        subgradeDefaultDataMapper.deleteById(id);
    }

    private void validateSubgradeDefaultDataExists(Long id) {
        if (subgradeDefaultDataMapper.selectById(id) == null) {
            throw exception(DATA_NOT_EXISTS);
        }
    }

    @Override
    public SubgradeDefaultDataDO getSubgradeDefaultData(Long id) {
        return subgradeDefaultDataMapper.selectById(id);
    }

    @Override
    public List<SubgradeDefaultDataDO> getSubgradeDefaultDataList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return subgradeDefaultDataMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SubgradeDefaultDataDO> getSubgradeDefaultDataPage(SubgradeDefaultDataPageReqVO pageReqVO) {
        return subgradeDefaultDataMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SubgradeDefaultDataDO> getSubgradeDefaultDataList(SubgradeDefaultDataExportReqVO exportReqVO) {
        return subgradeDefaultDataMapper.selectList(exportReqVO);
    }

}
